package com.scau.classOnline.service;

import com.scau.classOnline.common.TimetableStyle;
import com.scau.classOnline.dao.CourseMapper;
import com.scau.classOnline.dao.FileMapper;
import com.scau.classOnline.dao.ScheduleMapper;
import com.scau.classOnline.dto.ScheduleDTO;
import com.scau.classOnline.dto.ScheduleFileDTO;
import com.scau.classOnline.entity.Course;
import com.scau.classOnline.entity.File;
import com.scau.classOnline.entity.Schedule;
import com.scau.classOnline.model.ScheduleModel;
import com.scau.classOnline.utils.time.TimeFormatUtil;
import com.scau.classOnline.utils.time.TimePattern;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Create by ji on 2019-07-11
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private FileMapper fileMapper;

    public ScheduleModel getScheduleById(Integer id) {
        Schedule schedule = scheduleMapper.selectByPrimaryKey(id);
        ScheduleModel scheduleModel = new ScheduleModel();
        BeanUtils.copyProperties(schedule, scheduleModel);
        scheduleModel.setDate(TimeFormatUtil.convertDateToString(schedule.getDate(), TimePattern.DAY));

        Course course = courseMapper.selectByPrimaryKey(schedule.getCid());
        scheduleModel.setCid(course.getId());
        scheduleModel.setCourseTitle(course.getTitle());
        scheduleModel.setTimeInfo(TimetableStyle.timetableType[schedule.getTimeId()][1]);
        File file = fileMapper.selectByPrimaryKey(schedule.getFileid());
        scheduleModel.setFile(file);
        return scheduleModel;
    }

    @Transactional
    public boolean addSchedule(ScheduleDTO scheduleDTO) {
        Schedule schedule = new Schedule();
        BeanUtils.copyProperties(scheduleDTO, schedule);
        schedule.setDate(TimeFormatUtil.convertStringToDate(scheduleDTO.getDate(), TimePattern.DAY));
        int insert = scheduleMapper.insert(schedule);
        if (insert <= 0) {
            return false;
        }
        int incr = courseMapper.updateTotalByPrimaryKey(scheduleDTO.getCid());
        if (incr <= 0) {
            return false;
        }
        return true;
    }

    @Transactional
    public boolean updateScheduleById(ScheduleDTO scheduleDTO) {
        Schedule schedule = new Schedule();
        BeanUtils.copyProperties(scheduleDTO, schedule);
        schedule.setDate(TimeFormatUtil.convertStringToDate(scheduleDTO.getDate(), TimePattern.DAY));
        int update = scheduleMapper.updateByPrimaryKey(schedule);
        if (update <= 0) {
            return false;
        }
        return true;
    }

    @Transactional
    public boolean uploadCourseWare(ScheduleFileDTO scheduleFileDTO) {
        File file = new File();
        BeanUtils.copyProperties(scheduleFileDTO, file);
        int insert = fileMapper.insert(file);
        if (insert <= 0) {
            return false;
        }
        int result = scheduleMapper.updateFileIdByPrimaryKey(scheduleFileDTO.getSid(), file.getId());
        if (result <= 0) {
            return false;
        }
        return true;
    }

    public int updateAllByTime() {
        return scheduleMapper.updateAllByTime();
    }
}
